/**
 * Created by luojie on 2018/3/13.
 */
!(function () {

	var WX = {
		version: '2.0.5'
	};
	var shareData = {
		title: '',
		desc: '',
		link: '',
		imgUrl: '',
		success: function () {
		},
		cancel: function () {
		}
	};
	var isDebug = false;
	var isInit = false;
	var _currShareData = null;
	var initcallback = null;
	var wechatSdkUrl = "http://res.wx.qq.com/open/js/jweixin-1.0.0.js";
	WX.wechat = null;
	WX.setWechat = function (wechat) {
		WX.wechat = wechat;
	};
	var jsApiList= [
		'checkJsApi',
		'onMenuShareTimeline',
		'onMenuShareAppMessage',
		'onMenuShareQQ',
		'onMenuShareWeibo',
		'onMenuShareQZone',
		'hideMenuItems',
		'showMenuItems',
		'hideAllNonBaseMenuItem',
		'showAllNonBaseMenuItem',
		'translateVoice',
		'startRecord',
		'stopRecord',
		'onRecordEnd',
		'playVoice',
		'pauseVoice',
		'stopVoice',
		'uploadVoice',
		'downloadVoice',
		'chooseImage',
		'previewImage',
		'uploadImage',
		'downloadImage',
		'getNetworkType',
		'openLocation',
		'getLocation',
		'hideOptionMenu',
		'showOptionMenu',
		'closeWindow',
		'scanQRCode',
		'chooseWXPay',
		'openProductSpecificView',
		'addCard',
		'chooseCard',
		'openCard'
	];
	/**
	 * 鍒嗕韩鍒濆鍖�
	 * @param  {[type]} defaultshareData  榛樿鍒嗕韩鏂囨
	 * @param  {[type]} callback          鍒濆鍖栨垚鍔熷悗鍥炶皟鍑芥暟
	 * @param  {[type]} debug             璋冭瘯鏄惁鎵撳紑 榛樿false
	 */
	WX.init = function (defaultshareData, callback, debug) {
		defaultshareData = defaultshareData || {};
		shareData = extend(shareData, defaultshareData);
		debug = debug || isDebug;
		initcallback = typeof(callback) === "function" ? callback : null;
		isDebug = !!debug;
		var url = "http://www.hugaint.com/MAELWXSDK/getshare.php?t=" + new Date().getTime() + "&callback=tp.wx.config&url=" + encodeURIComponent(location.href.replace(location.hash, ""));

		if ("function" == typeof define) {
			require(["http://www.hugaint.com/MAELWXSDK/getshare.php?t=" + new Date().getTime() + "&callback=define&url=" + encodeURIComponent(location.href.replace(location.hash, ""))], function (data) {
				WX.config(data);
			});
		}
		else if (window.wx) {
			WX.setWechat(window.wx);
			loadScript(url);
		} else {
			loadScript(wechatSdkUrl, function () {
				WX.setWechat(window.wx);
				loadScript(url);
			})
		}
	};

	WX.config = function (d) {
		if (d.ret != 200) {
			if (isDebug)alert(JSON.stringify(d));
			return;
		}

		WX.wechat.config({
			debug: isDebug,
			appId: d.appid,
			timestamp: d.timestamp,
			nonceStr: d.noncestr,
			signature: d.signature,
			jsApiList: jsApiList
		});

		WX.wechat.ready(function () {
			WX.wechat.checkJsApi({
				jsApiList:jsApiList,
				success: function(res) {
					typeof (initcallback) === "function" && initcallback();
					WX.wechat.hideMenuItems({
						menuList: ['menuItem:share:weiboApp', 'menuItem:share:facebook']
					});
					WX.wechat.showMenuItems({
						menuList: ['menuItem:profile', 'menuItem:addContact']
					});
					isInit = true;

					WX.setshare(_currShareData);
				}
			});
		});
	};



	WX.setshare = function (d) {

		_currShareData = d || {};

		if (!isInit) {
			return;
		}

		var currShareData = extend(shareData, _currShareData);

		var _link = currShareData.link;

		// 鍒嗕韩鍒板井淇℃湅鍙嬪湀
		WX.wechat.onMenuShareTimeline({
			title: currShareData.title,
			link: _link,
			imgUrl: currShareData.imgUrl,
			success: function () {
				currShareData.success && currShareData.success();
				try {
					_hmt.push(['_trackEvent', "鍒嗕韩鎴愬姛", '鍒嗕韩鍒版湅鍙嬪湀']);
				} catch (e) {
				}
			},
			cancel: function () {
				currShareData.cancel && currShareData.cancel();
				try {
					_hmt.push(['_trackEvent', "鍙栨秷鍒嗕韩", '鍙栨秷鍒嗕韩']);
				} catch (e) {
				}
			}
		});

		// 鍙戦€佺粰鎸囧畾寰俊濂藉弸
		WX.wechat.onMenuShareAppMessage({
			title: currShareData.title,
			desc: currShareData.desc,
			link: _link,
			imgUrl: currShareData.imgUrl,
			success: function () {
				currShareData.success && currShareData.success();
				try {
					_hmt.push(['_trackEvent', "鍒嗕韩鎴愬姛", '鍒嗕韩缁欏ソ鍙�']);
				} catch (e) {
				}
			},
			cancel: function () {
				currShareData.cancel && currShareData.cancel();
				try {
					_hmt.push(['_trackEvent', "鍙栨秷鍒嗕韩", '鍙栨秷鍒嗕韩']);
				} catch (e) {
				}
			}
		});


		WX.wechat.onMenuShareQQ({
			title: currShareData.title, // 鍒嗕韩鏍囬
			desc: currShareData.desc, // 鍒嗕韩鎻忚堪
			link: _link.replace("hmsr={from}","hmsr=qq"), // 鍒嗕韩閾炬帴
			imgUrl: currShareData.imgUrl, // 鍒嗕韩鍥炬爣
			success: function () {
				currShareData.success && currShareData.success();
				try {
					_hmt.push(['_trackEvent', "鍒嗕韩鎴愬姛", '鍒嗕韩鎴愬姛']);
				} catch (e) {
				}
			},
			cancel: function () {
				currShareData.cancel && currShareData.cancel();
				try {
					_hmt.push(['_trackEvent', "鍙栨秷鍒嗕韩", '鍙栨秷鍒嗕韩']);
				} catch (e) {
				}
			}
		});
	};

	function loadScript(url, callback) {

		var script = document.createElement("script");
		script.type = "text/javascript";
		if (script.readyState) { //IE
			script.onreadystatechange = function () {
				if (script.readyState == "loaded" ||
					script.readyState == "complete") {
					script.onreadystatechange = null;
					typeof (callback) === "function" && callback();
				}
			};
		} else { //Others: Firefox, Safari, Chrome, and Opera
			script.onload = function () {
				typeof (callback) === "function" && callback();
			};
		}
		script.src = url;
		DOMReady(function () {
			document.body.appendChild(script);
		});

	}

	var isDOMReady = false;

	function DOMReady(callback) {
		if (isDOMReady) {
			typeof (callback) === "function" && callback();
		} else {
			setTimeout(function () {
				if (document.body) {
					isDOMReady = true;
				}
				DOMReady(callback);
			}, 1);
		}

	}

	function extend(target, source) {
		target = target || {};
		var result = {};
		for (var p in target) {
			if (source.hasOwnProperty(p)) {
				result[p] = source[p];
			} else {
				result[p] = target[p];
			}
		}
		return result;
	}

	"function" == typeof define ? define([wechatSdkUrl], function (wx) {
		WX.setWechat(wx);
		return WX;
	}) : (function () {
		window.tp = window.tp || {};
		window.tp['wx'] = WX;
	})();

})();